<template>
  <div id="contentBox" class="app-container">
    <div style="margin-bottom: 20px;">
      <el-input v-model="listQuery.activityName" placeholder="请输入问卷名字" style="width: 200px;" class="filter-item" clearable @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 20px;" @click="handleFilter">
        搜索
      </el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-plus" style="float:right" @click="$router.push('/marketing/questionnaire/questionsAdd')">
        创建问卷
      </el-button>
    </div>
    <el-table
      ref="filterTable"
      :data="questionsList"
      style="width: 100%"
      border
      fit
      :element-loading-text="g.loadingStr"
      :header-cell-style="{background:g.mainColor}"
    >
      <el-table-column prop="questionnaireName" label="问卷名" align="center">
        <template slot-scope="{row}">
          <span class="quesName" @click="quesInfo(row)">{{ row.questionnaireName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="answer" label="答卷" align="center">
        <!-- <template slot-scope="{row}"> -->
        <!-- <span>{{}}</span> -->
        <!-- </template> -->
      </el-table-column>
      <el-table-column prop="date" label="发布时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="{row}">
          <span>{{ row.status === 1 ? '已发布' : '未发布' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center" width="400px">
        <template slot-scope="{row,$index}">
          <el-button v-show="row.status===2" type="success" size="mini" @click="handleStatus(row,$index)">发布</el-button>
          <el-button v-show="row.status===2" type="info" size="mini" @click="handleEdit(row)">编辑</el-button>
          <el-button
            v-show="row.status === 1"
            type="primary"
            size="mini"
            @click="handleCheck(row)"
          >查看数据</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row,$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.currentPage"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import * as Question from '@/api/questions'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'QuestionsList',
  components: { Pagination },
  data() {
    return {
      orgId: 0,
      username: '',
      total: 0,
      questionsList: [],
      listQuery: {
        currentPage: 1,
        pageSize: 10
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      Question.getQuestionsList({ orgId: this.orgId }).then((res) => {
        console.log(res.data)
        this.total = res.data.length
        this.questionsList = res.data
      })
    },
    // 发布
    handleStatus(row, index) {
      this.$confirm(
        `是否确认${row.status === 1 ? '取消发布' : '发布'}该问卷？`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {})
    },
    // 编辑
    handleEdit(row) {
      this.$router.push({
        path: '/marketing/questionnaire/questionsEdit',
        query: {
          id: row.id
        }
      })
    },
    // 删除
    handleDelete(row, $index) {
      console.log(row)
      console.log($index)
      this.$confirm('是否要删除该问卷?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Question.deleteQuestionsList({ id: row.id }).then((res) => {
          if (res.code === '200') {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getList()
          }
        })
      })
    },
    // 查看
    handleCheck(row) {
      this.$router.push({
        path: '/marketing/questionnaire/questionsData',
        query: {
          id: row.id
        }
      })
    },
    handleFilter() {
      this.listQuery.currentPage = 1
      this.getList()
    },
    quesInfo(row) {
      console.log(row)
      const isPublish = row.status === 1
      if (isPublish) {
        this.$router.push({
          path: '/marketing/questionnaire/questionsInfo',
          query: {
            id: row.id
          }
        })
      } else {
        this.$router.push({
          path: '/marketing/questionnaire/questionsAdd',
          query: {
            id: row.id
          }
        })
      }
    }
  }
}
</script>

<style lang='scss' scoped>
#contentBox {
  padding: 10px 20px;
  .el-input-group {
    width: 200px;
    height: 30px;
    margin-left: 20px;
  }
  .searchHead {
    margin-bottom: 15px;
  }
  .quesName {
    cursor: pointer;
  }
}
</style>
